package com.znyj.servlet.dao;

import com.znyj.servlet.entity.FactoryEntity;
import com.znyj.servlet.sqlHelper.SqlProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * Created by gaoliwei02 on 17/7/31.
 */
@Repository
public interface FactoryDao {
    public static final String SELECT_KEYS = " " +
            "id," +
            "user_id," +
            "reborn," +
            "device_type," +
            "comm_type," +
            "country," +
            "iot_id," +
            "mcu_id," +
            "qam_id," +
            "pcb_id," +
            "mcu_sv," +
            "factory," +
            "imei," +
            "created_time," +
            "device_id";
    public static final String INSERT_KEYS = " " +
            "user_id," +
            "reborn," +
            "device_type," +
            "comm_type," +
            "country," +
            "iot_id," +
            "mcu_id," +
            "qam_id," +
            "pcb_id," +
            "mcu_sv," +
            "factory," +
            "imei," +
            "created_time," +
            "device_id";
    public static final String INSERT_VALUES = " " +
            "#{userId}," +
            "#{reborn}," +
            "#{deviceType}," +
            "#{commType}," +
            "#{country}," +
            "#{iotId}," +
            "#{mcuId}," +
            "#{qamId}," +
            "#{pcbId}," +
            "#{mcuSv}," +
            "#{factory}," +
            "#{imei}," +
            "#{createdTime}," +
            "#{deviceId}";

    @Insert("insert into cdb_factory (" + INSERT_KEYS + ") values (" + INSERT_VALUES + ") ")
    public int insert(FactoryEntity entity);

    @Update("update cdb_factory set " +
            "user_id=#{userId}," +
            "reborn=#{reborn}," +
            "device_type=#{deviceType}," +
            "comm_type=#{commType}," +
            "country=#{country}," +
            "iot_id=#{iotId}," +
            "mcu_id=#{mcuId}," +
            "qam_id=#{qamId}," +
            "pcb_id=#{pcbId}," +
            "mcu_sv=#{mcuSv}," +
            "factory=#{factory}," +
            "imei=#{imei}," +
            "created_time=#{createdTime} " +
            "where device_id=#{deviceId}")
    public int update(FactoryEntity entity);

    @Update("update cdb_factory set " +
            "reborn=#{reborn} " +
            "where device_id=#{deviceId}")
    public int updateReborn(@Param("reborn") String reborn, @Param("deviceId") String deviceId);

    @Select("select * from cdb_factory where imei=#{imei}")
    public FactoryEntity findByImei(@Param("imei") String imei);

    @Select("select count(*) from cdb_factory where imei=#{imei}")
    public int countByImei(@Param("imei") String imei);

    @Select("delete from cdb_factory where imei=#{imei}")
    public int deleteByImei(@Param("imei") String imei);

    @Select("select count(*) from cdb_factory where imei=#{imei}")
    public int isExistImei(@Param("imei") String imei);

    @SelectProvider(type = SqlProvider.class, method = "countByExp")
    public int countByExp(String exp);

}
